package com.teradyne.merlin2.ui.client.activities;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Date;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;

import com.google.gwt.activity.shared.AbstractActivity;
import com.google.gwt.event.shared.EventBus;
import com.google.gwt.place.shared.Place;
import com.google.gwt.user.cellview.client.ColumnSortList;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.AcceptsOneWidget;
import com.google.gwt.view.client.AsyncDataProvider;
import com.google.gwt.view.client.HasData;
import com.teradyne.merlin2.ui.client.service.AsyncCallbackCustom;
import com.teradyne.merlin2.ui.client.service.OrdersServiceAsync;
import com.teradyne.merlin2.ui.client.views.OrderListView;
import com.teradyne.merlin2.ui.shared.models.RequestStatus;

public class OrderListActivity extends AbstractActivity implements OrderListView.Presenter, ActivityCustom {

	private Logger logger = Logger.getLogger("OrderListActivity");

	private final OrderListView view;
	private final OrdersServiceAsync orderService;
	private Place place;

	public OrderListActivity(OrderListView view, OrdersServiceAsync orderService) {
		this.view = view;
		this.orderService = orderService;
	}

	@Override
	public void start(final AcceptsOneWidget container, EventBus eventBus) {
		container.setWidget(view.asWidget());
		view.setPresenter(OrderListActivity.this);
		queryListOfRequest();
	}

	private void queryListOfRequest() {
		logger.log(Level.INFO, "Before queryListOfRequest() call!" + new Date());
		orderService.getOrderRequestWithoutOrders("510", "00", new AsyncCallbackCustom<ArrayList<RequestStatus>>() {

			@Override
			public void onCustomSuccess(ArrayList<RequestStatus> result) {
				logger.log(Level.INFO, "After queryListOfRequest() call!" + new Date());
				populateDataListTable(result);
			}

			@Override
			public void onCustomFailure(Throwable caught) {
				this.setFailMessage("There was something wrong when contacting the server.");
			}
		});

	}

	private void populateDataListTable(final ArrayList<RequestStatus> result) {
		AsyncDataProvider<RequestStatus> provider = new AsyncDataProvider<RequestStatus>() {
			@Override
			protected void onRangeChanged(HasData<RequestStatus> display) {
				int start = display.getVisibleRange().getStart();
				int end = start + display.getVisibleRange().getLength();

				end = end >= result.size() ? result.size() : end;

				final ColumnSortList sortList = view.getOrderListTable().getColumnSortList();

				Collections.sort(result, new Comparator<RequestStatus>() {

					@Override
					public int compare(RequestStatus o1, RequestStatus o2) {
						if (o1 == o2) {
							return 0;
						}
						// Compare the ServiceType columns.
						int diff = -1;
						if (o1 != null) {
							diff = (o2 != null) ? o1.getServiceType().compareTo(o2.getServiceType()) : 1;
						}
						return sortList.get(0).isAscending() ? diff : -diff;
					}
				});

				List<RequestStatus> sub = result.subList(start, end);
				updateRowData(start, sub);
			}
		};

		provider.addDataDisplay(view.getOrderListTable());
		provider.updateRowCount(result.size(), true);

	}

	@Override
	public void setPlace(Place place) {
		this.place = place;
	}

	@Override
	public Place getPlace() {
		return place;
	}

}